package com.videoaggre.zcl.ui.activity.agefans

import android.content.pm.PackageManager
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.kingja.loadsir.core.LoadService
import com.videoaggre.zcl.R
import com.videoaggre.zcl.bean.AgefansHome
import com.videoaggre.zcl.common.bindViewClick
import com.videoaggre.zcl.common.showLoadingCallback
import com.videoaggre.zcl.common.startKtxActivity
import com.videoaggre.zcl.helper.loadsir.LoadSirHelper
import com.videoaggre.zcl.helper.newInstance
import com.videoaggre.zcl.parse.BaseParse
import com.videoaggre.zcl.parse.impl.AgefansParse
import com.videoaggre.zcl.ui.activity.BaseActivity
import com.videoaggre.zcl.ui.adapter.AgefansItemAdapter
import com.videoaggre.zcl.ui.dialog.showAlertDialog
import com.videoaggre.zcl.ui.fragment.parseHtml
import com.videoaggre.zcl.utils.FullyGridLayoutManager
import com.videoaggre.zcl.utils.Logger
import kotlinx.android.synthetic.main.agefans_activity_main.*

class AgefansMainActivity : BaseActivity(R.layout.agefans_activity_main) {
    private var mAgefansHome: AgefansHome? = null
    private val mLastUpdatedAdapter: AgefansItemAdapter by newInstance()
    private val mTodayRecommendedAdapter: AgefansItemAdapter by newInstance()
    private val mLoadService: LoadService<*> by lazy { LoadSirHelper.register(rootView) { onReload() } }
    override fun initView() {
        bindViewClick(this, titleView.rightImageView, viewFunLookup, viewFunTime, viewFunRank, tvLastUpdatedMore, tvTodayRecommendedMore)
        rvLastUpdated.layoutManager = FullyGridLayoutManager(this, 3)
        rvLastUpdated.adapter = mLastUpdatedAdapter
        rvTodayRecommended.layoutManager = FullyGridLayoutManager(this, 3)
        rvTodayRecommended.adapter = mTodayRecommendedAdapter
    }

    override fun onClick(v: View?) {
        when (v) {
            titleView.rightImageView -> startKtxActivity<AgefansSearchActivity>()
            tvLastUpdatedMore -> startKtxActivity<AgefansLastUpdatedActivity>()
            tvTodayRecommendedMore -> startKtxActivity<AgefansTodayRecommendedActivity>()
            viewFunLookup -> startKtxActivity<AgefansCatalogActivity>()
            viewFunRank -> startKtxActivity<AgefansRankActivity>()
            viewFunTime -> mAgefansHome?.let {
                startKtxActivity<AgefansTimeTableActivity>(extra = Bundle().apply {
                    putParcelableArrayList(AgefansTimeTableActivity.TIME_TABLE, it.timeTables)
                })
            }
        }
    }

    override fun initData() {
        if (!checkApkExist()) {
            showAlertDialog {
                message(message = "未安装微信，视频播放服务可能无法正常使用")
                negativeButton(name = "我知道了") { }
            }
        }
        mLoadService.showLoadingCallback()
        onReload()
    }

    private fun onReload() {
        parseHtml(BaseParse(this, this, AgefansParse.HOME, loadService = mLoadService) {
            AgefansParse.parseHome(it) {
                runOnUiThread {
                    mAgefansHome = it
                    mLastUpdatedAdapter.datas = it.lastUpdateds
                    mLastUpdatedAdapter.notifyDataSetChanged()
                    mTodayRecommendedAdapter.datas = it.todayRecommendeds
                    mTodayRecommendedAdapter.notifyDataSetChanged()
                }
            }
        })
    }

    fun checkApkExist(): Boolean {
        return if (TextUtils.isEmpty(packageName)) false else try {
            packageManager.getApplicationInfo("com.tencent.mm", PackageManager.GET_UNINSTALLED_PACKAGES)
            true
        } catch (e: PackageManager.NameNotFoundException) {
            false
        }
    }
}